//
//  HNStrengtheningClassApplicationCountView.m
//  优学荟管理系统
//
//  Created by edz on 2017/11/27.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "HNStrengtheningClassApplicationCountView.h"

@implementation HNStrengtheningClassApplicationCountView
{
    __weak IBOutlet UILabel *_Num; /** 数值 */
    __weak IBOutlet UILabel *_Title; /** 标题 */
    __weak IBOutlet UIView *_LineView; /** 灰色的View */
    __weak IBOutlet UIView *_ColorView; /** 有颜色的View */
    __weak IBOutlet NSLayoutConstraint *_ColorViewRightConstraint; /** 有颜色的View的右对齐的距离 */
    
    NSNumber *_TempNum;
    NSNumber *_TempCountNum;
}

- (instancetype)initWithNum:(NSNumber *)Num andTitle:(NSString *)Title andCountNum:(NSNumber *)CountNum {
    
    self = [[NSBundle mainBundle] loadNibNamed:[HNStrengtheningClassApplicationCountView description] owner:nil options:nil][0];
    if (self) {
        
        _TempNum = Num;
        _TempCountNum = CountNum;
        _Num.text = [Num stringValue];
        _Title.text = Title;
        _ColorView.backgroundColor = [self CountNumForColorWithScale:[Num floatValue]/[CountNum floatValue]];
    }
    return self;
}



/** 根据数值获取颜色 */
- (UIColor *)CountNumForColorWithScale:(CGFloat)Scale {
    
    if (Scale >= 0.75) {
        return ColorRGB(137, 231, 197);
    } else if (Scale < 0.78 && Scale >= 0.5) {
        return ColorRGB(113, 209, 238);
    } else if (Scale < 0.5 && Scale >= 0.25) {
        return ColorRGB(246, 201, 84);
    } else {
        return ColorRGB(233, 87, 78);
    }
}


- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    CGFloat Scale = [_TempNum floatValue] / [_TempCountNum floatValue];
    Scale = [_TempCountNum isEqualToNumber:@0] ? 0 : Scale;
    Scale = _LineView.frame.size.width * Scale;
    Scale = _LineView.frame.size.width - Scale;
    _ColorViewRightConstraint.constant = (-Scale);
}
@end
